<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="关联会员ID" prop="memberId">
        <el-input v-model="formData.memberId" placeholder="请输入关联会员ID" />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="formData.name" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="formData.sex">
          <el-radio
            v-for="dict in getIntDictOptions(DICT_TYPE.SYSTEM_USER_SEX)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="联系号码" prop="phone">
        <el-input v-model="formData.phone" placeholder="请输入联系号码" />
      </el-form-item>
      <el-form-item label="工作" prop="job">
        <el-input v-model="formData.job" placeholder="请输入工作" />
      </el-form-item>
      <el-form-item label="代理地区" prop="proxyArea">
        <el-tree-select
          v-model="formData.proxyArea"
          :data="areaList"
          :props="defaultProps"
          :render-after-expand="true"
        />
      </el-form-item>
      <el-form-item label="代理类型" prop="proxyType">
        <el-select v-model="formData.proxyType" placeholder="请选择代理类型">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.MEMBER_PROXY_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="申请年限" prop="askForYear">
        <el-input v-model="formData.askForYear" placeholder="请输入申请年限" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="formData.status" placeholder="请选择状态">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.SYSTEM_PROXY_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="代理等级" prop="proxyLevel">
        <el-input v-model="formData.proxyLevel" placeholder="请输入代理等级" />
      </el-form-item>
      <el-form-item label="返佣比例" prop="backRate">
        <el-input v-model="formData.backRate" placeholder="请输入返佣比例，0为不返佣，100为返回所有支付金额" />
      </el-form-item>
      <el-form-item label="主体资质" prop="qualification">
        <el-select v-model="formData.qualification" placeholder="请选择主体资质">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.MEMBER_PROXY_QUALIFICTION)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="申请人职位" prop="askerPosition">
        <el-input v-model="formData.askerPosition" placeholder="请输入申请人职位" />
      </el-form-item>
      <el-form-item label="公司名称" prop="companyName">
        <el-input v-model="formData.companyName" placeholder="请输入公司名称" />
      </el-form-item>
      <el-form-item label="公司地址" prop="companyAddress">
        <el-input v-model="formData.companyAddress" placeholder="请输入公司地址" />
      </el-form-item>
      <el-form-item label="法人" prop="legalPerson">
        <el-input v-model="formData.legalPerson" placeholder="请输入法人" />
      </el-form-item>
      <el-form-item label="公司性质" prop="companyType">
        <el-select v-model="formData.companyType" placeholder="请选择公司性质">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.SYSTEM_COMPANY_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="公司注册时间" prop="registeredTime">
        <el-date-picker
          v-model="formData.registeredTime"
          type="date"
          value-format="x"
          placeholder="选择公司注册时间"
        />
      </el-form-item>
      <el-form-item label="注册资金" prop="registeredCapital">
        <el-input v-model="formData.registeredCapital" placeholder="请输入注册资金" />
      </el-form-item>
      <el-form-item label="公司主营业务" prop="companyBusiness">
        <el-input v-model="formData.companyBusiness" placeholder="请输入公司主营业务" />
      </el-form-item>
      <el-form-item label="经验" prop="experience">
        <el-input v-model="formData.experience" type="textarea" placeholder="请输入经验" />
      </el-form-item>
      <el-form-item label="优势" prop="advantage">
        <Editor v-model="formData.advantage" height="150px" />
      </el-form-item>
      <el-form-item label="同意年限" prop="agreeYear">is
        <el-input v-model="formData.agreeYear" placeholder="请输入同意年限" />
      </el-form-item>
      <el-form-item label="过期时间" prop="expireTime">
        <el-date-picker
          v-model="formData.expireTime"
          type="date"
          value-format="x"
          placeholder="选择过期时间"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { ProxyUserApi, ProxyUserVO } from '@/api/member/proxyuser'
import * as AreaApi from '@/api/system/area'
import { defaultProps } from '@/utils/tree'
/** 代理用户信息 表单 */
defineOptions({ name: 'ProxyUserForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  memberId: undefined,
  name: undefined,
  sex: undefined,
  phone: undefined,
  job: undefined,
  proxyArea: undefined,
  proxyType: undefined,
  askForYear: undefined,
  status: undefined,
  proxyLevel: undefined,
  backRate: undefined,
  qualification: undefined,
  askerPosition: undefined,
  companyName: undefined,
  companyAddress: undefined,
  legalPerson: undefined,
  companyType: undefined,
  registeredTime: undefined,
  registeredCapital: undefined,
  companyBusiness: undefined,
  experience: undefined,
  advantage: undefined,
  agreeYear: undefined,
  expireTime: undefined
})
const formRules = reactive({
  memberId: [{ required: true, message: '关联会员ID不能为空', trigger: 'blur' }],
  name: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
  sex: [{ required: true, message: '性别不能为空', trigger: 'blur' }],
  phone: [{ required: true, message: '联系号码不能为空', trigger: 'blur' }],
  job: [{ required: true, message: '工作不能为空', trigger: 'blur' }],
  proxyArea: [{ required: true, message: '代理地区不能为空', trigger: 'blur' }],
  proxyType: [{ required: true, message: '代理类型不能为空', trigger: 'change' }],
  askForYear: [{ required: true, message: '申请年限不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '状态不能为空', trigger: 'change' }],
  proxyLevel: [{ required: true, message: '代理等级不能为空', trigger: 'blur' }],
  qualification: [{ required: true, message: '主体资质不能为空', trigger: 'change' }]
})
const formRef = ref() // 表单 Ref
const areaList = ref([]) // 地区列表
/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  // 获得地区列表
  areaList.value = await AreaApi.getAreaTree()
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ProxyUserApi.getProxyUser(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ProxyUserVO
    if (formType.value === 'create') {
      await ProxyUserApi.createProxyUser(data)
      message.success(t('common.createSuccess'))
    } else {
      await ProxyUserApi.updateProxyUser(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    memberId: undefined,
    name: undefined,
    sex: undefined,
    phone: undefined,
    job: undefined,
    proxyArea: undefined,
    proxyType: undefined,
    askForYear: undefined,
    status: undefined,
    proxyLevel: undefined,
    backRate: undefined,
    qualification: undefined,
    askerPosition: undefined,
    companyName: undefined,
    companyAddress: undefined,
    legalPerson: undefined,
    companyType: undefined,
    registeredTime: undefined,
    registeredCapital: undefined,
    companyBusiness: undefined,
    experience: undefined,
    advantage: undefined,
    agreeYear: undefined,
    expireTime: undefined
  }
  formRef.value?.resetFields()
}
</script>